<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\BaseController;
use App\Http\ApiResponse;
use App\Helpers\ManageResponse;

use App\Models\KeywordRecord;
use App\Models\QrCode;
use App\Models\Profit;
use App\Models\Member;

class MiniAppController extends BaseController
{
    use ManageResponse;

    protected $pageSize = 16;
    protected $pageNum = 1;
    const WEIXIN_PRICE = 58;   //直接获取微信方式
    const SUPER_PRICE = 32;   //超级喜欢金额
    const ENLIST_CHANGE_ABLUM_PRICE = 2; //交换相册

    /**
     * http请求的返回控制对象
     * @var ApiResponse \App\Http\ApiResponse
     */
    protected $apiResponse;

    public function __construct()
    {
        $request = request();
        $this->apiResponse = new ApiResponse($this);
        $this->pageNum = $request->input('page_num', 1);
        $this->pageSize = $request->get("limit", 16);
    }

    public function pre_attack($data)
    {
        if (is_array($data)) {
            foreach ($data as $key => $val) {
                $data [$key] = trim($val);
                $data [$key] = strip_tags($val);
                $data [$key] = htmlspecialchars($val);
                // $data [$key] = $this->my_addslashes($val);
            }
        } else {
            $data = trim($data);  //清理空格
            $data = strip_tags($data);   //过滤html标签
            $data = htmlspecialchars($data);   //将字符内容转化为html实体

            // $data = $this->my_addslashes($data);  //防止SQL注入
        }
        return $data;
    }

    function my_addslashes($var)
    {
        if (!get_magic_quotes_gpc()) {

            if (is_array($var)) {
                foreach ($var as $key => $val) {
                    $var [$key] = my_addslashes($val);
                }
            } else {
                $var = addslashes($var);
            }
        }
        return $var;
    }

    //渠道退款公共方法
    protected function partner_refund($uid, $source_type, $refund_amount, $is_activity = false)
    {
        if ($uid && $source_type && $refund_amount) {
            $qcid = KeywordRecord::where(['update_uid' => $uid, 'is_new' => 1])->value('qr_code_id');

            if ($qcid) {
                $qc = QrCode::where("id", "=", $qcid)->first();

                if ($qc['uid'] && $qc['partner_rate']) {
                    $pdata['uid'] = $uid;
                    $pdata['parent_uid'] = $qc['uid'];
                    $pdata['parent_qr_code_id'] = $qcid;
                    $pdata['source'] = $source_type;
                    $pdata['type'] = 0;//退款
                    $partner_rate = $qc['partner_rate'];
                    if ($is_activity) {//活动，返现比例除2
                        $partner_rate = $qc['partner_rate'] / 2;
                    }
                    $pdata['price'] = number_format($refund_amount * $partner_rate, 2);
                    $pdata['create_time'] = date("Y-m-d H:i:s");

                    Profit::insert($pdata);
                    $member = Member::where("id", "=", $qc['uid'])->first();
                    Member::where("id", "=", $qc['uid'])->update(['balance', bcsub($member['balance'], $pdata['price'], 2)]);//->increment('balance', number_format($pdata['price'], 2));
                    Member::where("id", "=", $qc['uid'])->update(['total_award', bcsub($member['total_award'], $pdata['price'], 2)]);//->increment('total_award', number_format($pdata['price'], 2));
                }
            }
        }
    }
}
